package com.bosssoft.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.entity.dto.FibDepartmentDTO;
import com.bosssoft.entity.dto.FibFiscalDTO;
import com.bosssoft.entity.po.FibFiscalPO;
import com.bosssoft.entity.vo.FibFiscalVO;
import com.bosssoft.entity.vo.FibDepartmentVO;
import com.bosssoft.entity.vo.FibFiscalVO;
import com.bosssoft.exception.DaoException;
import com.bosssoft.page.PageResult;
import com.bosssoft.response.Result;
import com.bosssoft.service.FibFiscalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.List;

/**
 * @author wahhh
 * @date 2021/8/20 13:19
 */
@Slf4j
@RestController
@ResponseBody
@RequestMapping("/fibFiscal")
public class FibFiscalController {
    @Resource
    private FibFiscalService fibFiscalService;

    @PostMapping("/select")
    public Result select(@Validated @RequestBody Long id) {
        try {
            FibFiscalDTO records = fibFiscalService.selectOneById(id);
            return Result.ok().message("查询成功").data("list",records);
        } catch (Exception e) {
            return Result.error().code(1).message("查询失败");
        } finally {
        }
    }

    @PostMapping("/selectDeleted")
    public boolean selectDeleted(@Validated @RequestBody Long id) {
        try {
            fibFiscalService.selectOneById(id);
            return fibFiscalService.selectDeleted(id);
        } catch (Exception e) {
            return false;
        } finally {
        }
    }

    @PostMapping("/selectAll")
    public Result selectAll(@RequestParam(defaultValue = "1") Integer pageNo,
                            @RequestParam(defaultValue = "10") Integer pageSize, FibFiscalVO fibFiscalVO) {
        try {
            FibFiscalPO fibFiscalPO = new FibFiscalPO();
            BeanUtils.copyProperties(fibFiscalVO,fibFiscalPO);
            Page<FibFiscalPO> page = new Page<>(pageNo,pageSize);
            PageResult<FibFiscalPO> pageList = fibFiscalService.page(page, fibFiscalPO);
            return Result.ok().data("page",pageList);
        } catch (DaoException e) {
            e.printStackTrace();
            return Result.error();
        }
    }
//    @PostMapping("/selectAll")
//    public Result selectAll(@RequestBody Long page,Long limit) {
//        try {
//            List<FibFiscalDTO> records = fibFiscalService.selectAll(page,limit);
//            return Result.ok().message("查询成功").data("list",records);
//        } catch (Exception e) {
//            return Result.error().code(1).message("查询失败");
//        } finally {
//        }
//    }
    
    /**
     * 根据id删除
     *
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@Validated @RequestBody FibFiscalVO fibFiscalVO) {
        FibFiscalDTO fibFiscalDTO = new FibFiscalDTO();
        BeanUtils.copyProperties(fibFiscalVO, fibFiscalDTO);
        boolean delete = fibFiscalService.delete(fibFiscalDTO);
        if (delete) {
            return Result.ok().code(1).message("删除账户成功");
        }else {
            return Result.error().code(2).message("删除账户失败");
        }
    }
    /**插入一条数据
     * @return*/
    @PostMapping("/insert")
    public Result insert(@Validated @RequestBody FibFiscalVO fibFiscalVO) {
        FibFiscalDTO fibFiscalDTO = new FibFiscalDTO();
        BeanUtils.copyProperties(fibFiscalVO, fibFiscalDTO);
        boolean insert = fibFiscalService.insert(fibFiscalDTO);
        if (insert) {
            return Result.ok().code(1).message("添加账户成功");
        }else {
            return Result.error().code(2).message("添加账户失败");
        }
    }
    /**更新数据
     * @return*/
    @PostMapping("/update")
    public Result update(@Validated @RequestBody FibFiscalVO fibFiscalVO) {
        FibFiscalDTO fibFiscalDTO = new FibFiscalDTO();
        BeanUtils.copyProperties(fibFiscalVO, fibFiscalDTO);
        boolean update = fibFiscalService.update(fibFiscalDTO);
        if (update) {
            return Result.ok().code(1).message("更新账户成功");
        }else {
            return Result.error().code(2).message("更新账户失败");
        }
    }
    /**
     * 更新激活状态
     * @return
     *
     */
    @PutMapping("/updateDeleted")
    public Result updateDeleted(@Validated @RequestBody FibFiscalVO fibFiscalVO){
        FibFiscalDTO fibFiscalDTO = new FibFiscalDTO();
        BeanUtils.copyProperties(fibFiscalVO, fibFiscalDTO);
        boolean update = fibFiscalService.updateDeleted(fibFiscalDTO);
        if (update) {
            return Result.ok().code(1).message("更新账户激活状态成功");
        }else {
            return Result.error().code(2).message("更新账户激活状态失败");
        }
    }

}
